package com.hezhi.project.module.student.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hezhi.project.module.student.domain.Student;
import com.hezhi.project.module.student.service.IStudentService;
import com.hezhi.framework.web.controller.BaseController;
import com.hezhi.framework.web.page.TableDataInfo;
import com.hezhi.framework.web.domain.AjaxResult;

/**
 * 学生 信息操作处理
 * 
 * @author hezhi
 * @date 2018-08-06
 */
@Controller
@RequestMapping("/module/student")
public class StudentController extends BaseController
{
    private String prefix = "module/student";
	
	@Autowired
	private IStudentService studentService;
	
	@RequiresPermissions("module:student:view")
	@GetMapping()
	public String student()
	{
	    return prefix + "/student";
	}
	
	/**
	 * 查询学生列表
	 */
	@RequiresPermissions("module:student:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Student student)
	{
		startPage();
        List<Student> list = studentService.selectStudentList(student);
		return getDataTable(list);
	}
	
	/**
	 * 新增学生
	 */
	@RequiresPermissions("module:student:add")
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 修改学生
	 */
	@RequiresPermissions("module:student:edit")
	@GetMapping("/edit/{studentNumber}")
	public String edit(@PathVariable("studentNumber") String studentNumber, Model model)
	{
		Student student = studentService.selectStudentById(studentNumber);
		model.addAttribute("student", student);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存学生
	 */
	@RequiresPermissions("module:student:save")
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Student student)
	{
		if (studentService.saveStudent(student) > 0)
		{
			return success();
		}
		return error();
	}
	
	/**
	 * 删除学生
	 */
	@RequiresPermissions("module:student:remove")
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		int rows = studentService.deleteStudentByIds(ids);
		if (rows > 0)
        {
            return success();
        }
        return error();
	}
	
	
	/**
	 * 验证学号唯一
	 * @param grade
	 * @return
	 */
	@PostMapping("/checkStudentNumber")
	@ResponseBody
	public String checkStudentNumber(Student student){
		if(student != null && !"".equals(student.getStudentNumber()) && student.getStudentNumber() != null){
			if(studentService.selectStudentById(student.getStudentNumber())!=null){
				return "1";
			}
		}
		return "0";
	}
}
